﻿using System;
using System.Management;
using System.Management.Automation;
using Microsoft.SharePoint;

namespace SPPoSh.Cmdlets.Artefacts.Lists
{
    [Cmdlet(VerbsCommon.Get, "SPList")]
    public class GetSPListCmdlet:Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true, HelpMessage = "URL of the web containing the list.")]
        public string Web;

        [Parameter(Position = 1, Mandatory = true, HelpMessage = "Name of the list.")]
        public string Title;

        protected override void ProcessRecord()
        {
            using (SPSite site = new SPSite(this.Web))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[this.Title];
                    this.WriteObject(list, false);
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.New, "SPList")]
    public class NewSPListCmdlet : Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true, HelpMessage = "URL of the web where to create the new list.")]
        public string Web;

        [Parameter(Position = 1, Mandatory = true, HelpMessage = "Name of the new list to create.")]
        public string Title;

        [Parameter(Position = 2, Mandatory = false, HelpMessage = "Template type for the list to create (Custom List, Document Library, etc.).")]
        public SPListTemplateType Type;

        [Parameter(Position = 3, Mandatory = false, HelpMessage = "Description for the new list.")]
        public string Description;

        protected override void ProcessRecord()
        {
            using(SPSite site = new SPSite(this.Web))
            {
                using(SPWeb web = site.OpenWeb())
                {
                    Guid listGUID = web.Lists.Add(this.Title, this.Description, this.Type);
                    SPList list = web.Lists[listGUID];
                    WriteObject(list, false);
                }
            }
        }
    }

    /// <summary>
    /// Moves a list in its entirety and with all of its content from one location to another, inside the same site collection.
    /// </summary>
    [Cmdlet(VerbsCommon.Move, "SPList")]
    public class MoveSPListCmdlet:Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true, HelpMessage = "URL of the source web where the list to move is located.")]
        public string Web;

        [Parameter(Position = 1, Mandatory = true, HelpMessage = "Name of the source list.")]
        public string Title;

        [Parameter(Position = 2, Mandatory = false, HelpMessage = "URL of the destination web (same site collection has source.")]
        public string Destination;

        [Parameter(Position = 3, Mandatory = false, HelpMessage = "Name of the destination list. Same as source if empty.")]
        public string NewTitle;

        protected override void ProcessRecord()
        {
            if (this.NewTitle == string.Empty)
                this.NewTitle = this.Title;

            using(SPSite site = new SPSite(this.Web))
            {
                using(SPWeb web = site.OpenWeb())
                {
                    SPList originalList = web.Lists[this.Title];
                    System.Random rnd = new Random(System.DateTime.Now.Millisecond);
                    string listTemplateName = "SPPoSh - SPMoveList - " + rnd.Next().ToString();

                    originalList.SaveAsTemplate(listTemplateName, listTemplateName, "Temporary template Generated from SPPoSh SP-MoveList operation", true);
                    
                    SPListTemplateCollection templateColl = site.GetCustomListTemplates(web);
                    SPListTemplate newTemplate = templateColl[listTemplateName];                    
                    
                    using(SPWeb destWeb = site.OpenWeb(this.Destination))
                    {
                        Guid newLsitGUID = destWeb.Lists.Add(this.NewTitle, originalList.Description, newTemplate);
                        SPList newList = destWeb.Lists[newLsitGUID];
                        SPList listTemplateList = site.RootWeb.Lists[""];
                        WriteObject(newList, false);
                    }
                    
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Remove, "SPList")]
    public class RemoveSPListCmdlet : Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true, HelpMessage = "URL of the web containing the list to delete.")]
        public string Web;

        [Parameter(Position = 1, Mandatory = true, HelpMessage = "Name of the list to delete.")]
        public string List;

        protected override void ProcessRecord()
        {
            using(SPSite site = new SPSite(this.Web))
            {
                using(SPWeb web = site.OpenWeb())
                {
                    web.Lists[this.List].Delete();
                }
            }
        }
    }

    [Cmdlet(VerbsCommon.Set, "SPList")]
    public class SetSPListCmdlet:Cmdlet
    {
        [Parameter(Position = 0, Mandatory = true, HelpMessage = "URL of the web containing the list.")]
        public string Web;

        [Parameter(Position = 1, Mandatory = true, HelpMessage = "Name of the list to modify.")]
        public string Title;

        [Parameter(Position = 2, Mandatory = false, HelpMessage = "New name for the list.")]
        public string NewTitle;

        protected override void ProcessRecord()
        {
            using(SPSite site = new SPSite(this.Web))
            {
                using(SPWeb web =site.OpenWeb())
                {
                    SPList list = web.Lists[this.Title];
                    list.Title = this.NewTitle;
                    this.WriteObject(list, false);
                }
            }
        }
    }
}
